package com.qianbao.qianbaobusiness.ui.cashier;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.androidbase.widget.recycleadpter.BaseRecycleViewAdapter;
import com.baseandroid.config.Constant;
import com.qianbao.qianbaobusiness.BuildConfig;
import com.qianbao.qianbaobusiness.R;
import com.qianbao.qianbaobusiness.json.DataListBean;
import com.tencent.stat.StatService;

/**
 * @author: Routee
 * @date 2018/6/27
 * @mail wangc4@qianbaocard.com
 * ------------1.本类由Routee开发,阅读、修改时请勿随意修改代码排版格式后提交到git。
 * ------------2.阅读本类时，发现不合理请及时指正.
 * ------------3.如需在本类内部进行修改,请先联系Routee,若未经同意修改此类后造成损失本人概不负责。
 */
public class CashierAdapter extends BaseRecycleViewAdapter<DataListBean> {

    public CashierAdapter(Context context) {
        super(context);
    }

    @Override
    protected void onBindBaseViewHolder(RecyclerView.ViewHolder holder, DataListBean item) {
        VH vh = (VH) holder;
        vh.mTvName.setText(item.getMerchantName());
        if (TextUtils.equals(item.getTradeStatus(), "2")) {
            vh.mTvCashier.setTextColor(ContextCompat.getColor(mContext, R.color.text_red));
            vh.mTvStatue.setTextColor(ContextCompat.getColor(mContext, R.color.text_support));
            vh.mTvStatue.setText("交易成功");
        } else if (TextUtils.equals(item.getTradeStatus(), "3")) {
            vh.mTvCashier.setTextColor(ContextCompat.getColor(mContext, R.color.text_support));
            vh.mTvStatue.setTextColor(ContextCompat.getColor(mContext, R.color.text_color_balance_header));
            vh.mTvStatue.setText("交易失败");
        } else if (TextUtils.equals(item.getTradeStatus(), "4")) {
            vh.mTvCashier.setTextColor(ContextCompat.getColor(mContext, R.color.text_support));
            vh.mTvStatue.setTextColor(ContextCompat.getColor(mContext, R.color.text_color_balance_header));
            vh.mTvStatue.setText("已撤销");
        } else if (TextUtils.equals(item.getTradeStatus(), "5")) {
            vh.mTvCashier.setTextColor(ContextCompat.getColor(mContext, R.color.text_support));
            vh.mTvStatue.setTextColor(ContextCompat.getColor(mContext, R.color.text_color_balance_header));
            vh.mTvStatue.setText("用户支付中");
        } else if (TextUtils.equals(item.getTradeStatus(), "6")) {
            vh.mTvCashier.setTextColor(ContextCompat.getColor(mContext, R.color.text_support));
            vh.mTvStatue.setTextColor(ContextCompat.getColor(mContext, R.color.text_color_balance_header));
            vh.mTvStatue.setText("已冲正");
        } else if (TextUtils.equals(item.getTradeStatus(), "7")) {
            vh.mTvCashier.setTextColor(ContextCompat.getColor(mContext, R.color.text_support));
            vh.mTvStatue.setTextColor(ContextCompat.getColor(mContext, R.color.text_color_balance_header));
            vh.mTvStatue.setText("冲正失败");
        } else if (TextUtils.equals(item.getTradeStatus(), "8")) {
            vh.mTvCashier.setTextColor(ContextCompat.getColor(mContext, R.color.text_support));
            vh.mTvStatue.setTextColor(ContextCompat.getColor(mContext, R.color.text_color_balance_header));
            vh.mTvStatue.setText("撤销成功");
        } else if (TextUtils.equals(item.getTradeStatus(), "9")) {
            vh.mTvCashier.setTextColor(ContextCompat.getColor(mContext, R.color.text_support));
            vh.mTvStatue.setTextColor(ContextCompat.getColor(mContext, R.color.text_color_balance_header));
            vh.mTvStatue.setText("撤销失败");
        }
        if (TextUtils.equals(item.getTradeStatus(), "2")) {
            vh.mTvCashier.setText("+" + item.getTradeAmt());
        } else {
            vh.mTvCashier.setText(item.getTradeAmt().replace("-","-"));
        }
        if (TextUtils.equals(item.getPayType(), Constant.ORDER_TYPE_CARD)) {
            vh.mIvType.setImageResource(R.drawable.icon_paytype_card);
        } else if (TextUtils.equals(item.getPayType(), Constant.ORDER_TYPE_ZHIFUBAO)) {
            vh.mIvType.setImageResource(R.drawable.icon_paytype_zhifubao);
        } else if (TextUtils.equals(item.getPayType(), Constant.ORDER_TYPE_WEIXIN)) {
            vh.mIvType.setImageResource(R.drawable.icon_paytype_weixin);
        } else if (TextUtils.equals(item.getPayType(), Constant.ORDER_TYPE_QIANBAO)) {
            vh.mIvType.setImageResource(R.drawable.icon_paytype_qianbao);
        }
        vh.mTvBhh.setVisibility(TextUtils.equals(item.getPayWay(), Constant.ORDER_TYPE_BHH) ? View.VISIBLE : View.GONE);
        if (holder.getAdapterPosition() == getItemCount() - 1) {
            vh.mSeparator.setVisibility(View.INVISIBLE);
        } else {
            vh.mSeparator.setVisibility(View.VISIBLE);
        }
        vh.mTvTime.setText(item.getTradeDate());
        vh.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, CashierDetailActivity.class);
                intent.putExtra("bean", item);
                mContext.startActivity(intent);
                if (!BuildConfig.DEBUG) {
                    StatService.trackCustomEvent(mContext, "3_0_home_cash_item");
                }
            }
        });
    }

    @Override
    protected RecyclerView.ViewHolder onCreateBaseViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_cashier, null);
        return new VH(view);
    }

    @Override
    protected int getBaseItemViewType(int position) {
        return 0;
    }

    class VH extends RecyclerView.ViewHolder {

        ImageView mIvType;
        TextView  mTvName;
        TextView  mTvBhh;
        TextView  mTvTime;
        TextView  mTvCashier;
        TextView  mTvStatue;
        View      mSeparator;

        public VH(View itemView) {
            super(itemView);
            mIvType = itemView.findViewById(R.id.iv_type);
            mTvName = itemView.findViewById(R.id.tv_merchant_name);
            mTvBhh = itemView.findViewById(R.id.tv_bhh);
            mTvTime = itemView.findViewById(R.id.tv_time);
            mTvCashier = itemView.findViewById(R.id.tv_cashier);
            mTvStatue = itemView.findViewById(R.id.tv_statue);
            mSeparator = itemView.findViewById(R.id.separator);
        }
    }
}
